package com.xiaoshuidi.cloud.module.rooms.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoshuidi.cloud.framework.common.exception.ServiceException;
import com.xiaoshuidi.cloud.framework.common.exception.enums.GlobalErrorCodeConstants;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.roomequipmentsupplier.vo.EquipmentSupplierPageReqVO;
import com.xiaoshuidi.cloud.module.rooms.mapper.EquipmentSupplierMapper;
import com.xiaoshuidi.cloud.module.rooms.pojo.EquipmentSupplier;
import com.xiaoshuidi.cloud.module.rooms.repository.irepository.IEquipmentSupplierRepository;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.List;


/**
 * @Author Feng
 * @date 2023/4/27
 */
@Repository
@RequiredArgsConstructor
public class EquipmentSupplierRepository extends ServiceImpl<EquipmentSupplierMapper, EquipmentSupplier> implements IEquipmentSupplierRepository {

    private final EquipmentSupplierMapper mapper;

    @Override
    public PageResult<EquipmentSupplier> pageQuery(EquipmentSupplierPageReqVO reqVO) {
        LambdaQueryWrapper<EquipmentSupplier> wrapper = new LambdaQueryWrapper<EquipmentSupplier>()
                .like(StringUtils.isNoneEmpty(reqVO.getKeyWord()),EquipmentSupplier::getName,reqVO.getKeyWord())
                .orderByDesc(EquipmentSupplier::getCreateTime);
        return mapper.selectPage(reqVO, wrapper);
    }

    @Override
    public Boolean validateSupplierName(String name) {
         List<EquipmentSupplier> list = mapper.selectList(new LambdaQueryWrapper<EquipmentSupplier>().eq(EquipmentSupplier::getName,name));
        if(ObjectUtils.isNotEmpty(list)){
            throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),"供应商名称已存在");
        }
        return true;
    }
}
